package com.xinxing.city.recovery.rfid;

import android.util.Log;

import com.xinxing.city.recovery.APPRuntime;
import com.xinxing.city.recovery.DI;
import com.xinxing.city.recovery.entitys.Catalogs;
import com.xinxing.city.recovery.entitys.KernelAction;
import com.xinxing.city.recovery.entitys.KernelActions;
import com.xinxing.city.recovery.interfaces.IDeviceKernel;

import com.xinxing.city.recovery.interfaces.IRfidManager;
import com.xinxing.city.recovery.interfaces.ISerialSendReceiveData;
import com.xinxing.city.recovery.serialport.SerialPortHelper;
import com.xinxing.city.recovery.utils.StringUtil;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class RfidManager implements IRfidManager {
    private SerialPortHelper2 serialPortHelper;
    private APPRuntime appRuntime;
    private List<String> rfids ;

    @Override
    public void start(String sPort, int iBaudRate) {
        appRuntime = DI.getInstance(APPRuntime.class);
        rfids = appRuntime.getRfids();
        List<Catalogs> catalogs = appRuntime.getCatalogs();
        //根据品类idCodePrefix 来判断是否rfid匹配
        try {
            serialPortHelper = new SerialPortHelper2(sPort, iBaudRate) {
                @Override
                protected void onDataReceived(byte[] buffer, int size) {
                    String res2 = StringUtil.byte2HexStr(buffer);
                    Log.e("rfid", res2);
                    if(res2.contains("7e")){
                        String[] res = res2.split("7e");
                        if(res.length>0){
                            for(String s : res){
                                if(s.length()>=14&&s.substring(0,2).equals("bb")&&s.substring(8,10).equals("11")){
                                    String strs = s.substring(16,16+24);
                                    if(!rfids.contains(strs)){
                                        rfids.add(strs);
                                    }
                                }
                            }
                            if(rfids.size()>0){
                                EventBus.getDefault().post(new KernelAction(KernelActions.RFID_READ, null));
                            }
                        }
                    }
                }
            };
            serialPortHelper.open();
        } catch (IOException e) {
            Log.e("Err",e.toString());
        }
    }

    @Override
    public void stop() {
        serialPortHelper.close();
    }

    @Override
    public void singleRfid() {
        ArrayList<Byte> mbuffer = new ArrayList<>();
        mbuffer.add((byte) 0xBB);
        mbuffer.add((byte) 0x00);
        mbuffer.add((byte) 0x22);
        mbuffer.add((byte) 0x00);
        mbuffer.add((byte) 0x00);
        mbuffer.add((byte) 0x22);
        mbuffer.add((byte) 0x7E);
        sendtoPortBuffer(mbuffer);
    }

    @Override
    public void loopRfid() {

    }

    @Override
    public List<String> getRfid() {
        return rfids;
    }

    public void sendtoPortBuffer(List<Byte> mBuffer) {
        byte[] bytes = new byte[mBuffer.size()];
        for (int i = 0; i < mBuffer.size(); i++) {
            bytes[i] = mBuffer.get(i);
        }
        String res = StringUtil.byte2HexStr(bytes);
        Log.e("rfid","发送 : "+res);
        serialPortHelper.send(bytes);

    }
}
